﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BoardPlacer : MonoBehaviour
{
    [Range(0,5)]
    public int Index;

    public Board.BoardType BoardType;
    private float m_DefaultZ = 100f;

    public bool IsOwnSide;
    private static bool m_NeedToReset = false;
    public bool CanPlace = true;
    private CardProps.ID m_CurrID;
    // Update is called once per frame
    void Start() {
        transform.position = new Vector3(transform.position.x, transform.position.y, m_DefaultZ);
    }
    void Update() {
        if(!CanPlace) GetComponent<Collider>().enabled = false;
        else GetComponent<Collider>().enabled = true;
    }
    public void OnMouseRayHit(CardProps.ID id) {
        Show_HalfAlpha(id);
        m_NeedToReset = false;
    }
    private void Show_HalfAlpha(CardProps.ID id) {
        GetComponent<SpriteRenderer>().sprite = CardFactory.Instance.GetLittleSprite(id);
        GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f, 0.2f);
    }
    public void ClearState() {
        //GetComponent<SpriteRenderer>().sprite = null;
        GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f, 0f);
    }
    
    public void RefreshState() {
        Card cardData;
        Side side;
        if(IsOwnSide) {
            side = VSController.Instance.GetCurrSide();
            CanPlace = true;
        }
        else {
            side = VSController.Instance.GetOppositeSide();
            CanPlace = false;
        }
        if(BoardType == Board.BoardType.EXPOSED) {
            cardData = side.ExposedField[Index];
        } else {
            cardData = side.HiddenField[Index];
        }
        if(cardData) {
            GetComponent<SpriteRenderer>().sprite = CardFactory.Instance.GetLittleSprite(cardData.ID);
            GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            if(IsOwnSide) 
                CanPlace = false;
            else if(BoardType == Board.BoardType.HIDDEN) {
                GetComponent<SpriteRenderer>().color = new Color(1.0f, 1.0f, 1.0f, 0.2f);
            }
        } else {
            GetComponent<SpriteRenderer>().sprite = null;
        }
    }
    /*void OnChangePlacer() {
            Debug.Log("Exit");
            ClearState();
            //StartCoroutine(ClearPlacerStateCoroutine());
    }
    IEnumerator ClearPlacerStateCoroutine() {
        m_NeedToReset = true;
        yield return new WaitForSeconds(0.2f);
        if(m_NeedToReset) {
            MouseRayCaster.Instance.ReShowCardUI();
            m_NeedToReset = false;
        }
    } */
}
